package com.example.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Question {
    private Integer id;
    private String content;//题目内容

    private Integer correctAnswerIndex;//正确答案索引

    @JsonIgnore
    private ObjectMapper objectMapper = new ObjectMapper();
    private List<String> options;//选项列表

    public String getOptionsAsJson() {
        try {
            return objectMapper.writeValueAsString(options);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Error converting options to JSON", e);
        }
    }
    // 将options列进行初始化
    public void setOptions(String jsonOptions) {
        CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(List.class, String.class);
        try {
            this.options = objectMapper.readValue(jsonOptions, listType);
        } catch (Exception e) {
            throw new RuntimeException("Error converting JSON to options", e);
        }
    }
}
